package com.xy6.struct.tree;

import java.util.List;


/**
 * 多叉树测试
 * 
 * @author zhang
 * @since 2017-09-10
 */
public class ManyNodeTreeTest {

	public static void main(String[] args) {
//		test2();
		test1();
//		test3();
	}
	
	/**
	 * 测试多叉树遍历顺序
	 */
	public static ManyNodeTree test1(){
		ManyNodeTree tree = new ManyNodeTree();
		TreeNode root = new TreeNode("1", null);
		tree.setRoot(new ManyTreeNode(root));
		tree.addChild(new TreeNode("2","1"));
		tree.addChild(new TreeNode("4","2"));
		tree.addChild(new TreeNode("5","2"));
		
		tree.addChild(new TreeNode("3","1"));
		tree.addChild(new TreeNode("6","3"));
		tree.addChild(new TreeNode("7","6"));
		
		print(tree.getNodes());
		print(tree.getNodesBFS());
		
		return tree;
	}
	
	/**
	 * 测试多叉树创建、添加节点
	 */
	public static void test2(){
		ManyNodeTree tree = new ManyNodeTree();
		TreeNode root = new TreeNode("root", null);
		ManyTreeNode mroot = new ManyTreeNode(root);
		tree.setRoot(mroot);
		print(tree.getNodes());
		
		tree.addChild(new TreeNode("system manager", "root"));
		print(tree.getNodes());
		tree.addChild(new TreeNode("user manager", "system manager"));
		tree.addChild(new TreeNode("log manager", "system manager"));
		print(tree.getNodes());
	}

	/**
	 * 输出每个节点信息
	 */
	public static void print(List<ManyTreeNode> nodes) {
		if (nodes == null || nodes.size() == 0) {
			System.out.println("empty tree");
			return;
		}
		for (ManyTreeNode elem : nodes) {
			System.out.println(String.format("cur node: %s, parent: %s", elem.getData().getNodeId(), elem.getData()
					.getParentId()));
		}
		System.out.println("#############");
	}
	
	public static void test3(){
		ManyNodeTree tree = test1();
		print(tree.getNodes());
	}

}
